<template>
  <div class="main-container">
    <div class="text">
        添加商品
    </div>
    <el-form :model="editForm" style="width: 400px;margin-top: 20px;" :rules="rules" ref="formRef">
      <el-form-item label="商品名称" label-width="80px" prop="name">
        <el-input v-model="editForm.name" />
      </el-form-item>
      <el-form-item label="商品分类" label-width="80px">
        <el-select v-model="editForm.category" placeholder="请选择商品分类">
          <el-option  
          v-for="(item, index) in cateName.categories"  
          :key="index"  
          :label="item.cateName"  
          :value="item.cateName"  
          style="width: 200px;"
      />    
        </el-select>
      </el-form-item>
      <el-form-item label="商品价格" label-width="80px">
      <el-input-number v-model="editForm.price" :min="1"/>
    </el-form-item>
    <el-form-item label="商品图片" label-width="80px">
      <AddGoodsImg  @update="updateImageUrl"/>
    </el-form-item>
    <el-form-item label="商品描述" label-width="80px">
      <el-mention
    v-model="editForm.goodsDesc"
    type="textarea"
    style="width: 320px"
    placeholder="Please input"
    />
    </el-form-item>
      
        <el-form-item label-width="80px">
          <el-button type="primary" @click="submitForm" >提交</el-button>
        </el-form-item>
    </el-form>

  </div>
</template>

<script lang="ts" setup>

import { ref,reactive } from 'vue'
import { ElMessage } from 'element-plus'
import {getCategories,goodsAdd} from '@/api/goods'
import AddGoodsImg from '@/views/components/AddGoodsImg.vue'
import type {FormRules ,FormInstance} from 'element-plus'

interface Goods {
  name: string
  category: string
  price: number
  imgUrl: string
  goodsDesc: string
}
const editForm = ref<Goods>({
  name: '',
  category: '',
  price: 0,
  imgUrl: '',
  goodsDesc: ''
})
let cateName= ref([]);

const formRef = ref<FormInstance>(); 

const rules = reactive<FormRules< Goods>>({
  name: [{  required:true, message: '请输入商品名称', trigger: 'blur' }],
  category: [{ required:true, message: '请选择商品分类', trigger: 'blur' }],
  price: [{ required:true,  message: '请输入商品价格',trigger: 'blur' }],
  imgUrl: [{ required:true, message: '请输入有效数字', trigger: 'blur' }],
  goodsDesc: [{ required:true,  message: '请填写商品描述',trigger: 'blur' }],
})

getCategories(
 
).then(response => {
 
  
   cateName.value = response;
   console.log(response.categories[0].cateName);
   
    console.log(cateName.value);
})

const updateImageUrl = (url: string) => {
  editForm.value.imgUrl = url
  console.log('更新后的图片 URL:', url);
}

const submitForm = async () => {
  // if (!editForm.value.name || !editForm.value.category || !editForm.value.price || !editForm.value.imgUrl || !editForm.value.goodsDesc) {
  //   ElMessage.error('请填写完整的商品信息')
  //   console.log(editForm.value);
    
  //   return
  // }
  try {
    const response = await goodsAdd(editForm.value)
    if (response.code === 0) {
      ElMessage.success('商品添加成功！')
      // 重置表单
      editForm.value = {
        name: '',
        category: '',
        price: 0,
        imgUrl: '',
        goodsDesc: ''
      }
    } else {
      ElMessage.error('商品添加失败，请稍后重试')
    }
  } catch (error) {
    ElMessage.error('网络错误，请稍后重试')
  }
}

</script>

<style scoped>

.text{
    width: 100%;
    height: 50px;
    border-bottom: 2px solid #f0f2f5;
    margin-bottom: 20px;
    font-size: 20px;
    line-height: 50px;
  }
.goods-name,.goods-desc,.price {
  margin: 20px 0;
}
.goods-desc, .uploader {
  display: flex;
}


</style>


